﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_OutStock : ATPages
{
    Function f = new Function();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            EditDate.Text = DateTime.Now.ToShortDateString();

            Hidid.Value = Server.UrlDecode(Request.Cookies["user"].Values["id"]);
            Path.Text = f.getModuleidAdd(Request["moduid"]);
            hidModuid.Value = Request["moduid"];
            initUser();
            Dh.Text = initBarCode();
            hidsj.Value = initBarbs();
            Hidsj2.Value = hidsj.Value;
            // initGifts();
            initGvData();
            Name.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            btnAdd.Attributes.Add("onclick", "return checkSale(this.form)");
            btnAd.Attributes.Add("onclick", "return check(this.form)");
        }
    }


    protected void linkbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("SalesManage.aspx?moduid=" + Request["moduid"]);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ZSql sales = new ZSql();
        ZSql dele = new ZSql();
        dele.Open("select * from CS_ls where LS_dh='" + hidsj.Value + "'");
        string dcode = initBarCode();
        for (int i = 0; i < dele.m_table.Rows.Count; i++)
        {

            string sql = "insert into SV_OutStock(OutCode,TypeId,comodBarcode,Units,ProcMoney,Prices,TotalMoney,OutNum,maintmen,Remark,EditUserName,EditDate,AuditState)values('" + dcode + "'," + dele.m_table.Rows[i]["LS_type"].ToString() + ",'" + dele.m_table.Rows[i]["LS_BarCode"].ToString() + "','" + dele.m_table.Rows[i]["LS_unit"].ToString() + "'," + dele.m_table.Rows[i]["LS_ProcMoney"].ToString() + "," + dele.m_table.Rows[i]["LS_practical"].ToString() + "," + dele.m_table.Rows[i]["LS_TotalPractical"].ToString() + "," + dele.m_table.Rows[i]["LS_num"].ToString() + "," + Common.FormatParameter(maintmen.SelectedValue) + ",'" + Remark.Text + "','" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "','" + EditDate.Text + "',0)";
          
            sales.Open(sql);
        }
        dele.Open("delete CS_ls where LS_dh='" + hidsj.Value + "'");//清除临时销售表
        sales.Close();
        dele.Close();

        Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
        Response.Write("<script>window.location='OutManage.aspx?moduid=" + hidModuid.Value + "';</script>");//返回页面
    }

    private void cler()
    {
        
        maintmen.SelectedIndex = 0;
        Remark.Text = "";
    }


    private string initBarCode()//销售正式流水号
    {
        ZSql zs = new ZSql();
        string id = "PJ" + DateTime.Now.ToString("yyyyMMdd");
        zs.Open("select isnull(max(OutStockId),0) as a from SV_OutStock");
        id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        zs.Close();
        return id;
    }

    private string initBarbs()//销售临时流水号
    {
        string a = "ls";
        Random r = new Random();
        a += DateTime.Now.Second.ToString() + r.Next(1000000).ToString();
        return a;
    }
    private void initGvData()
    {
        ZSql zs = new ZSql();
        zs.Open("select * from CS_ls where LS_dh='" + hidsj.Value + "'");
        gvData.DataSource = zs.m_table;
        gvData.DataBind();
        zs.Close();
    }
    protected void btnAd_Click(object sender, EventArgs e)//添加商品临时数据
    {
        ZSql sale = new ZSql();
        string sql = "insert into CS_ls(LS_dh,LS_band,LS_type,LS_name,LS_num,LS_unit,LS_modul,LS_practical,LS_TotalPractical,LS_ProcMoney,LS_BarCode)values('" + hidsj.Value + "','" + TypeName.Value + "','" + HidTypeId.Value + "','" + catalogueName.Value + "'," + OutNum.Value + ",'" + Units.Value + "','" + Spec.Value + "'," + Prices.Value + "," + TotalMoney.Value + "," + ProcMoney.Value + ",'" + ComodBarcode.Value + "')";
        sale.Open(sql);
        sale.Close();
        clearData();

        initGvData();
        initTable();
    }

    protected void linkDel_Click(object sender, EventArgs e)//删除数据
    {
        string id = (sender as LinkButton).CommandArgument;
        ZSql del = new ZSql();
        del.Open("delete CS_ls where id=" + id);
        del.Close();
        initGvData();
        initTable();

    }
    private void initTable()
    {
        ZSql sel = new ZSql();

        sel.Open("select sum(LS_num)as num,sum(LS_TotalPractical)as TotalPractical from CS_ls where ls_dh='" + hidsj.Value + "'");
        if (sel.m_table.Rows.Count > 0)
        {
            Xnum.Text = sel.m_table.Rows[0]["num"].ToString();
            Xmoney.Text = sel.m_table.Rows[0]["TotalPractical"].ToString();
            HidCountNum.Value = sel.m_table.Rows[0]["num"].ToString(); //总数
            sel.Close();
        }
        else
        {
            Xnum.Text = "0";
            Xmoney.Text = "0";
            HidCountNum.Value = "0";
        }
    }

    private void initUser()
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=40 order by StaffName");
        maintmen.DataSource = name.m_table;
        maintmen.DataTextField = "StaffName";
        maintmen.DataValueField = "StaffId";
        maintmen.DataBind();
        maintmen.Items.Insert(0, new ListItem("选择人员", ""));
        name.Close();
    }

    private void clearData()//返回清除数据
    {
        Spec.Value = "";
        ComodBarcode.Value = "";
        TypeName.Value = "";
        catalogueName.Value = "";
        Prices.Value = "0";
        OutNum.Value = "0";//初使化数量
    }
}


